﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WLFramework.DataAccess;
using WLFramework.Validation;
using WLFramework.Json;
using WLFramework.Web;
using GenericHtml;

namespace SampleSite.admin.ajax
{
    /// <summary>
    /// updateDep 的摘要说明
    /// </summary>
    public class updateDep : AjaxHttpHandler
    {
        public class InputData
        {
            [Required]
            public int O_ID { get; set; }

            [Required]
            public string O_CName { get; set; }

            public string O_EName { get; set; }
        }

        public override void OnProcessRequest(HttpContext context)
        {
            InputData input;
            var errors = context.GetModel<InputData>(out input);
            if (errors.Count == 0)
            {
                using (PermissionContext db = new PermissionContext())
                {

                    var accessor = DataAccessor.CreateInstance<PermissionCommand>(db);
                    db.BeginTransaction();

                    var result = accessor.UpdateDep(
                        input.O_ID,
                        input.O_CName,
                        input.O_EName);

                    if (result.ErrorID != 0)
                        throw new Exception(result.ErrorMsg);

                    db.CommitTransaction();
                }

                context.Response.Write(JSON.Instance.ToJSON(JSONResult.Success));
            }
            else
            {
                throw new Exception("输入的数据格式不正确");
            }
        }
    }
}